package com.meida.student.app;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.utils.JsonUtils;
import com.meida.msg.client.model.BaseMessage;
import com.meida.msg.client.model.EmailMessage;
import com.meida.msg.client.model.EmailTplMessage;
import com.meida.msg.client.entity.EmailConfig;
import com.meida.msg.client.entity.EmailLogs;
import com.meida.msg.client.entity.EmailTemplate;
import com.meida.msg.provider.service.EmailConfigService;
import com.meida.msg.provider.service.EmailLogsService;
import com.meida.msg.provider.service.EmailSender;
import com.meida.msg.provider.service.EmailTemplateService;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import jodd.mail.Email;
import jodd.mail.MailServer;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class SendGmail {

    public static final String mail_smtp_auth = "true";
    public static final String mail_transport_protocol = "smtp";
    public static final String mail_smtp_ssl_enable = "true";
    public static final String mail_smtp_socketFactory_class = "javax.net.ssl.SSLSocketFactory";
    public static final String mail_InternetAddress="support@turingedtech.com";
    public static final String	mail_Host = "smtp.gmail.com";
    public static final int	mail_port = 465;
    public static final String	mail_username = "support@turingedtech.com";//邮箱号
    public static final String	mail_password = "Turingoffer2021";  //登陆密码


    /**
     * 发送邮件
     *
     * @param sentTo
     *            收件人
     * @param mailTitle
     *            邮件主题
     * @param mailContent
     *            邮件内容
     */
    public boolean sendOneMail(String sentTo, String mailTitle, String mailContent) {
        Properties props = new Properties();
        props.setProperty("mail.smtp.auth",mail_smtp_auth);
        props.setProperty("mail.transport.protocol",mail_transport_protocol);
        props.setProperty("mail.smtp.ssl.enable",mail_smtp_ssl_enable);
        props.put("mail.smtp.socketFactory.class",mail_smtp_socketFactory_class);

        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(true);
        InternetAddress[] addres = new InternetAddress[1];
        try {
            // 设置收件人列表
            addres[0] = new InternetAddress(sentTo);
            // 创建邮件
            MimeMessage msg = new MimeMessage(session);
            // 发送的邮箱地址
            msg.setFrom(new InternetAddress(mail_InternetAddress));
            // 创建邮件的接收者地址，并设置到邮件消息中
            msg.setRecipients(Message.RecipientType.TO, sentTo);

            msg.setSubject(mailTitle);
            // 向multipart对象中添加邮件的各个部分内容，包括文本内容和附件
            Multipart multipart = new MimeMultipart();
            // 添加邮件正文
            BodyPart contentPart = new MimeBodyPart();
            contentPart.setText(mailContent);
            multipart.addBodyPart(contentPart);
            msg.setContent(multipart);
            // 保存邮件
            msg.saveChanges();
            // 创建发送对象
            Transport transport = session.getTransport();
            // 设置服务器以及账号和密码
            transport.connect(mail_Host,mail_port,mail_username, mail_password);
            // 发送到的邮箱地址
            transport.sendMessage(msg, addres);

            // 关闭
            transport.close();
        } catch (Exception e) {
            // e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
       // SendGmail sendGmail=	new SendGmail();
       // sendGmail.sendOneMail("574501933@qq.com", "测试gmail", "gmail发送邮件");

        SmtpServer smtpServer = MailServer.create()
                .ssl(true)
                .host("smtp.gmail.com")
                .auth("support@turingedtech.com", "Turingoffer2021")
                .buildSmtpMailServer();

        Email email = Email.create()
                .from("support@turingedtech.com")
                .to("574501933@qq.com")
                .subject("hello")
                .textMessage("Hello world!");

        SendMailSession session = smtpServer.createSession();
        session.open();
        session.sendMail(email);
        session.close();

    }

}